package com.debao.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.debao.common.enums.GoodsOrderStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.debao.system.mapper.JoinOrderMapper;
import com.debao.system.domain.JoinOrder;
import com.debao.system.service.IJoinOrderService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 定金支付订单Service业务层处理
 *
 * @author DeBao
 * @date 2024-05-31
 */
@Service
@Slf4j
public class JoinOrderServiceImpl implements IJoinOrderService {
    @Autowired
    private JoinOrderMapper joinOrderMapper;

    /**
     * 查询定金支付订单
     *
     * @param id 定金支付订单主键
     * @return 定金支付订单
     */
    @Override
    public JoinOrder selectJoinOrderById(Long id) {
        return joinOrderMapper.selectJoinOrderById(id);
    }

    /**
     * 根据订单号查询
     *
     * @param orderNo
     * @return
     */
    @Override
    public JoinOrder selectJoinOrderByOrderNo(String orderNo) {
        return joinOrderMapper.selectJoinOrderByOrderNo(orderNo);
    }


    /**
     * 查询定金支付订单列表
     *
     * @param joinOrder 定金支付订单
     * @return 定金支付订单
     */
    @Override
    public List<JoinOrder> selectJoinOrderList(JoinOrder joinOrder) {
        return joinOrderMapper.selectJoinOrderList(joinOrder);
    }

    /**
     * 处理支付成功回调
     *
     * @param orderNo
     * @param transactionId
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public void paySucHandleOrder(String orderNo, String transactionId) {
        if (StringUtils.isBlank(orderNo)) {
            log.error("订单回调更新数据失败, orderNo为空");
            return;
        }
        String logHead = String.format("订单回调更新, orderNo = %s ", orderNo);
        //获取订单信息
        JoinOrder joinOrder = joinOrderMapper.selectJoinOrderByOrderNo(orderNo);
        if (Objects.isNull(joinOrder)) {
            // todo 自定义异常类
            log.error(logHead + "订单不存在");
            return;
        }
        if (joinOrder.getStatus() != 0) {
            log.error(logHead + "订单信息回调已更新 status = {}", joinOrder.getStatus());
            return;
        }
        // 更新订单状态 【支付成功】
        joinOrder.setTransactionId(transactionId);
        joinOrder.setPayAt(new Date());
        joinOrder.setStatus(1);
        int row = joinOrderMapper.updateJoinOrder(joinOrder);
        if(row!= 1){
            log.error(logHead + "订单更新失败");
        }
    }

    /**
     * 订单是否支付成功
     *
     * @param orderNo
     * @return
     */
    @Override
    public boolean isPaySuccess(String orderNo) {
        return joinOrderMapper.isPaySuccess(orderNo);
    }

    /**
     * 新增定金支付订单
     *
     * @param joinOrder 定金支付订单
     * @return 结果
     */
    @Override
    public int insertJoinOrder(JoinOrder joinOrder) {
        return joinOrderMapper.insertJoinOrder(joinOrder);
    }

    /**
     * 修改定金支付订单
     *
     * @param joinOrder 定金支付订单
     * @return 结果
     */
    @Override
    public int updateJoinOrder(JoinOrder joinOrder) {
        return joinOrderMapper.updateJoinOrder(joinOrder);
    }

    /**
     * 批量删除定金支付订单
     *
     * @param ids 需要删除的定金支付订单主键
     * @return 结果
     */
    @Override
    public int deleteJoinOrderByIds(Long[] ids) {
        return joinOrderMapper.deleteJoinOrderByIds(ids);
    }

    /**
     * 删除定金支付订单信息
     *
     * @param id 定金支付订单主键
     * @return 结果
     */
    @Override
    public int deleteJoinOrderById(Long id) {
        return joinOrderMapper.deleteJoinOrderById(id);
    }

    /**
     * 定金是否已经支付
     * @param userId
     * @return
     */
    @Override
    public boolean isPay( Long userId) {
        return joinOrderMapper.isPay(userId);
    }
}
